package com.sgb.merchant.view.activity;

import android.text.TextUtils;
import android.util.TypedValue;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;


import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.fondesa.recyclerviewdivider.DividerDecoration;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;

import com.sgb.merchant.databinding.MerchantActivityEnrolListBinding;
import com.sgb.merchant.view.ApplyDialog;
import com.sgb.net.entity.ListResponse;
import com.swgk.core.base.model.entity.RouterKey;
import com.sgb.link.entity.merchant.EnrolInfoEntiy;
import com.sgb.link.router.PathMerchant;
import com.sgb.merchant.R;
import com.sgb.merchant.view.adapter.EnrolListAdapter;
import com.sgb.merchant.viewmodel.EnrolListViewModel;

import com.swgk.core.common.base.BaseActivity;
import com.swgk.core.dialog.DialogHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: 王彬臣
 * @date: 2021/7/16
 * @Description: 报名单位
 */
@Route(path = PathMerchant.EnrolListActivity)
public class EnrolListActivity extends BaseActivity<EnrolListViewModel, MerchantActivityEnrolListBinding> {
    @Autowired(name = RouterKey.NO, desc = "详情no")
    long no;
    @Autowired(name = RouterKey.TITLE)
    String title;
    @Autowired(name = RouterKey.BOOLEAN)
    boolean isSelf;
    //列表数据
    private int mPage = 1;
    List<EnrolInfoEntiy> mList=new ArrayList<>();
    private EnrolListAdapter mAdapter;

    @Override
    protected int layoutId() {
        return R.layout.merchant_activity_enrol_list;
    }

    @Override
    protected void createViewModel() {
        mViewModel=new ViewModelProvider(this).get(EnrolListViewModel.class);
    }

    @Override
    protected void init() {
        ARouter.getInstance().inject(this);
        mBinding.includeTitle.ivBack.setOnClickListener(v -> {
            finish();
        });
        mBinding.recycler.setLayoutManager(new LinearLayoutManager(this));
        DividerDecoration.builder(this)
                .color(getResources().getColor(R.color.color_f5f5f5))
                .insets(45,45)
                .size(1, TypedValue.COMPLEX_UNIT_DIP)
                .build()
                .addTo(mBinding.recycler);
        mAdapter = new EnrolListAdapter(mList);
        if(isSelf){
            mAdapter.setSelf(true);
        }
        mAdapter.bindToRecyclerView(mBinding.recycler);
        mAdapter.setEmptyView(R.layout.merchant_item_empty_view);
        if(TextUtils.isEmpty(title)) {
            mBinding.includeTitle.titleView.setText("报名单位");
        }else {
            mBinding.includeTitle.titleView.setText("报名记录");
            mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    EnrolInfoEntiy mEntity=mList.get(position);
                    new ApplyDialog(EnrolListActivity.this,mEntity).show();
                }
            });

        }
        mBinding.refreshLayout.setEnableRefresh(true);
        mBinding.refreshLayout.setEnableLoadMore(true);
        mBinding.refreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                mViewModel.getEnrolList(no,mPage);
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                mPage = 1;
                mViewModel.getEnrolList(no,mPage);
            }
        });
        DialogHelper.showProgressDialog(this, "加载中").setCanceledOnTouchOutside(true);
        mViewModel.getEnrolList(no,mPage);
        mViewModel.ldList.observe(this,this::syncData);
    }
    public void syncData(ListResponse<List<EnrolInfoEntiy>> entity) {
        mBinding.refreshLayout.finishRefresh();
        mBinding.refreshLayout.finishLoadMore();
        if (mPage == 1) {
            mList.clear();
        }
        if (entity.getList() != null) {
            mList.addAll(entity.getList());
        }
        mAdapter.notifyDataSetChanged();


        //
        if (!entity.isLastPage()) {
            mPage = entity.getCurrentPage() + 1;
            mBinding.refreshLayout.resetNoMoreData();
        } else {
            mBinding.refreshLayout.finishLoadMoreWithNoMoreData();
        }
    }

    public void showApplyDialog(EnrolInfoEntiy entity){

    }
}
